package com.tigger.common.utils;

import com.google.common.collect.Maps;
import com.tigger.common.cipher.AesEncipher;
import com.tigger.common.exception.BaseException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.Map;

/**
 * Created by root on 2017/9/13.
 */
public class WebParameter {

    final static Logger LOGGER = LoggerFactory.getLogger(WebParameter.class);

    /**
     * 获取请求参数的Bean
     *
     * @param request
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T getParamsBean(HttpServletRequest request, Class<T> clazz) {
        T bean = null;
        try {
            bean = clazz.newInstance();
            Enumeration<String> enums = request.getParameterNames();
            while (enums.hasMoreElements()) {
                String key = enums.nextElement();
                String val = request.getParameter(key);
                if (!StringUtils.isEmpty(val))
                    ReflectUtils.setFieldValueIgnoreCase(bean, key, val);
            }
        } catch (InstantiationException e) {
            LOGGER.debug(e.getMessage());
        } catch (IllegalAccessException e) {
            LOGGER.debug(e.getMessage());
        }
        return bean;
    }

    /**
     * 解密并获取参数的Bean
     *
     * @param request
     * @param paramName
     * @param clazz
     * @param <T>
     * @return
     * @throws BaseException
     */
    public static <T> T getParamsBeanWithDecipher(HttpServletRequest request, String paramName, Class<T> clazz) throws BaseException {
        Map<String, Object> paramsMap = getParamsMapWithDecipher(request, paramName);
        T bean = JSONUtils.map2Object(paramsMap, clazz);
        return bean;
    }

    /**
     * 解密并获取参数的Bean
     *
     * @param request
     * @param clazz
     * @param <T>
     * @return
     * @throws BaseException
     */
    public static <T> T getParamsBeanWithDecipherDefault(HttpServletRequest request, Class<T> clazz) throws BaseException {
        Map<String, Object> paramsMap = getParamsMapWithDecipher(request, "requestParam");
        T bean = JSONUtils.map2Object(paramsMap, clazz);
        return bean;
    }

    /**
     * 获取请求参数
     *
     * @param request
     * @return
     * @throws BaseException
     */
    public static Map<String, Object> getParamsMap(HttpServletRequest request) throws BaseException {
        Map<String, Object> paramsMap = Maps.newLinkedHashMap();
        Enumeration<String> enums = request.getParameterNames();
        while (enums.hasMoreElements()) {
            String key = enums.nextElement();
            String val = request.getParameter(key);
            if (!StringUtils.isEmpty(val))
                if (StringUtils.equals("pageNum", key) || StringUtils.equals("pageSize", key)) {
                    int intVal = Integer.valueOf(val);
                    paramsMap.put(key, intVal);
                } else {
                    paramsMap.put(key, val);
                }

        }
        return paramsMap;
    }

    /**
     * 解密并获取请求参数Map
     *
     * @param request
     * @param paramName
     * @return
     * @throws BaseException
     */
    public static Map<String, Object> getParamsMapWithDecipher(HttpServletRequest request, String paramName) throws BaseException {
        Map<String, Object> paramsMap = Maps.newLinkedHashMap();
        String reqParams = request.getParameter(paramName);
        if (StringUtils.isEmpty(reqParams))
            throw new BaseException(ResultCode.PARAMETER_NAME_ERROR, ResultCode.PARAMETER_NAME_ERROR_MSG);
        paramsMap = JSONUtils.toMap(AesEncipher.decipher(reqParams));
        return paramsMap;
    }

    /**
     * 解密并获取请求参数Map
     *
     * @param request
     * @return
     * @throws BaseException
     */
    public static Map<String, Object> getParamsMapWithDecipherDefalut(HttpServletRequest request) throws BaseException {
        return getParamsMapWithDecipher(request, "requestParam");
    }


}
